<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Basic_library
{
	// Protected or private properties
	protected $_table;
	
	// Constructor
	public function __construct()
	{
		if (!isset($this->CI))
		{
			$this->CI =& get_instance();
		}
		
		$this->_table = $this->CI->config->item('database_tables');
	}
		
	// Public methods
	
	public function get_category_name($id=null)
	{
		
		$this->CI->db->select('id, name');
		$this->CI->db->from('site_category');		
		$this->CI->db->where('id',$id);		
		$query = $this->CI->db->get();
			
		if ($query->num_rows() > 0)
		{			
			$return = $query->result_array();
		}
		else
		{
			return null;
		}
		
		if(is_array($return))
		{
			return $return[0]['name'];
		}
		return null;
	}
	
	public function get_categories_name($id=null, $alias = null)
	{
		
		$this->CI->db->select('id, name, alias');
		$this->CI->db->from('site_category');		
		$this->CI->db->where('id',$id);
		$this->CI->db->or_where('alias',$alias);		
		//$this->CI->db->where("(id = '" . $id . "' OR alias = '" . $alias . "')");		
		$query = $this->CI->db->get();
			
		if ($query->num_rows() > 0)
		{			
			$return = $query->result_array();
		}
		else
		{
			return null;
		}
		
		if(is_array($return))
		{
			return $return[0]['name'];
		}
		return null;
	}
	
	public function get_category_id($alias = null)
	{
		
		$this->CI->db->select('id, alias');
		$this->CI->db->from('site_category');		
		$this->CI->db->where('alias',$alias);		
		//$this->CI->db->where("(id = '" . $id . "' OR alias = '" . $alias . "')");		
		$query = $this->CI->db->get();
			
		if ($query->num_rows() > 0)
		{			
			$return = $query->result_array();
		}
		else
		{
			return null;
		}
		
		if(is_array($return))
		{
			return $return[0]['id'];
		}
		return null;
	}
	
	function count_all_student_by_class($id)
	{
		$this->CI->db->select('id');
		$this->CI->db->from('hocvien_lophoc');
		$this->CI->db->where('idlop',$id);
		return $this->CI->db->count_all_results();
	}
	
	function count_all_subject_by_class($lopid)
	{
		$this->CI->db->select('id');
		$this->CI->db->from('lophoc_monhoc');
		$this->CI->db->where('id_lop',$lopid);
		return $this->CI->db->count_all_results();
	}
	
	function count_all_class_by_teacher($id)
	{
		$this->CI->db->select('id');
		$this->CI->db->from('phanmon');
		$this->CI->db->where('gv_id',$id);
		$this->CI->db->group_by('lop_id',$id);
		return $this->CI->db->count_all_results();
	}
	
	public function get_category_info($id = null, $alias = null)
	{
		
		$this->CI->db->select('id, alias, name, description');
		$this->CI->db->from('site_category');		
		$this->CI->db->where('id',$id);
		$this->CI->db->or_where('alias',$alias);		
		$query = $this->CI->db->get();
			
		if ($query->num_rows() > 0)
		{			
			$return = $query->result_array();
		}
		else
		{
			return null;
		}
		
		if(is_array($return))
		{
			return $return[0];
		}
		return null;
	}
	
	public function get_categories_alias($id=null)
	{
		
		$this->CI->db->select('*');
		$this->CI->db->from('site_category');		
		$this->CI->db->where('id',$id);		
		$query = $this->CI->db->get();
			
		if ($query->num_rows() > 0)
		{			
			$return = $query->result_array();
		}
		else
		{
			return null;
		}
		
		if(is_array($return))
		{
			return $return[0]['alias'];
		}
		return null;
	}
	
	
	
	public function get_article_title($id=null)
	{
		
		$this->CI->db->select('id, description, title');
		$this->CI->db->from('site_content');		
		$this->CI->db->where('id',$id);		
		$query = $this->CI->db->get();
			
		if ($query->num_rows() > 0)
		{			
			$return = $query->result_array();
		}
		else
		{
			return null;
		}
		
		if(is_array($return))
		{
			return $return[0];
		}
		return null;
	}
	
	
	public function get_teacher_name($teacherid=null)
	{
		$this->CI->db->select('magv,tengv');
		$this->CI->db->from('giaovien');		
		$this->CI->db->where('id',$teacherid);		
		$query = $this->CI->db->get();
		return  $query->row();
	}
	
	public function get_teacher_name_by_user_id($user_id=null)
	{
		$this->CI->db->select('magv,tengv');
		$this->CI->db->from('giaovien');
		$this->CI->db->where('user_id',$user_id);
		$query = $this->CI->db->get();
		return  $query->row();
	}
	
	public function get_full_name($teacherid=null)
	{
		$this->CI->db->select('fullname');
		$this->CI->db->from('user');
		$this->CI->db->where('id',$teacherid);
		$query = $this->CI->db->get();
		return  $query->row();
	}
	
	public function get_khoa_name($teacherid=null)
	{
		$this->CI->db->select('giaovien.magv,giaovien.tengv, khoa.tenkhoa');
		$this->CI->db->from('giaovien','khoa');
		$this->CI->db->join('khoa','khoa.id = giaovien.id_khoa');
		$this->CI->db->where('giaovien.id',$teacherid);
		$query = $this->CI->db->get();
		return  $query->row();
	}
	
	public function get_store_name($khoid=null)
	{
		$this->CI->db->select('name');
		$this->CI->db->from('vt_kho');
		$this->CI->db->where('id',$khoid);
		$query = $this->CI->db->get();
		return  $query->row();
	}
	
	public function get_group_name($id=null)
	{
		
		$this->CI->db->select('*');
		$this->CI->db->from('role');		
		$this->CI->db->where('role_id',$id);		
		$query = $this->CI->db->get();
			
		if ($query->num_rows() > 0)
		{			
			$return = $query->result_array();
		}
		else
		{
			return null;
		}
		
		if(is_array($return))
		{
			return $return[0]['role_name'];
		}
		return null;
	}
	
	function get_total_mark_of_students_by_class($idhv, $mon_id)
	{
		$this->CI->db->select('bangdiem.diemtrungbinh');
		$this->CI->db->from('bangdiem');
		$this->CI->db->where('bangdiem.id_mon',$mon_id);
		$this->CI->db->where('bangdiem.id_hv',$idhv);
		$query = $this->CI->db->get();
		if ($query->num_rows() > 0)
		{
			return $query->row();
		}
		else 
		{
			return NULL;
		}
	}
	
	function get_status_info_by_class_and_mon($idlop, $idmon)
	{
		$this->CI->db->select('lophoc_monhoc.lock');
		$this->CI->db->from('lophoc_monhoc');
		$this->CI->db->where('lophoc_monhoc.id_mon',$idmon);
		$this->CI->db->where('lophoc_monhoc.id_lop',$idlop);
		$query = $this->CI->db->get();
		if ($query->num_rows() > 0)
		{
			return $query->row();
		}
		else
		{
			return NULL;
		}
	}
	
	
	function get_class_by_student($idhv)
	{
		$this->CI->db->select('lophoc.id, lophoc.malop, lophoc.tenlop');
		$this->CI->db->from('lophoc','hocvien_lophoc');
		$this->CI->db->join('hocvien_lophoc','hocvien_lophoc.idlop = lophoc.id');
		$this->CI->db->where('hocvien_lophoc.idhv',$idhv);
		$query = $this->CI->db->get();
		if ($query->num_rows() > 0)
		{
			return $query->result_array();
		}
		else
		{
			return NULL;
		}
	}
	
}

/* End of file Categories_library.php */
/* Location: ./application/libraries/Basic_library.php */